/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.guicommon.util;

public final class StringUtil {
	private StringUtil() {
	}
	
	public static boolean isDelimiter(final char c)
	{
		// check most frequent characters first
		if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
			return false; 
		}
		if (c >= '0' && c <= '9') {
			return false;	 
		}
		if (c == '.' || c == ',') {
			return false;	 
		}
		
		if ((c == ' ') || (c == '\n') || (c == '\t') || (c == '\r')) {
			return true;
		}

		return false;
	}
	
	public static String cleanString(final String text)
	{
		final StringBuilder result = new StringBuilder(text.length());
		
		boolean lastCharWasDelimiter = true;
		for (char c : text.toCharArray()) {
			final boolean currentCharIsDelimiter = isDelimiter(c);
			if (currentCharIsDelimiter) {
				if (!lastCharWasDelimiter) {
					result.append(' ');
					lastCharWasDelimiter = true;
				}
				continue;
			}
			
			lastCharWasDelimiter = false;
			result.append(c);
		}
		
		return result.toString().trim();
	}
}
